package com.ruoyi.web.controller.kaoyan;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.BaseEntityPlus;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.kaoyan.domain.EducationChoicePlan;
import com.ruoyi.kaoyan.service.EducationChoicePlanService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.controller.kaoyan.vo.PlanVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 前台考研广场计划列表
 *
 * @author carpediem
 */
@Api(tags = "前台考研广场计划列表", value = "前台考研广场计划列表")
@RestController
@RequestMapping("/v2/plan")
public class PlanApi extends BaseController {

    @Autowired
    private EducationChoicePlanService planService;
    @Autowired
    private ISysUserService userService;

    @ApiOperation("广场考研计划列表")
    @PostMapping("/list")
    public AjaxResult planList(String targetCity, String targetMajorName, String targetSchoolName, Long userId) {
        LambdaQueryWrapper<EducationChoicePlan> queryWrapper = new LambdaQueryWrapper<EducationChoicePlan>()
                .like(StringUtils.isNotBlank(targetCity), EducationChoicePlan::getTargetCity, targetCity)
                .like(StringUtils.isNotBlank(targetMajorName), EducationChoicePlan::getTargetMajorName, targetMajorName)
                .like(StringUtils.isNotBlank(targetSchoolName), EducationChoicePlan::getTargetSchoolName, targetSchoolName)
                .orderByDesc(true, BaseEntityPlus::getCreateTime);
        if (StringUtils.isNotNull(userId)){
            queryWrapper.eq(true, EducationChoicePlan::getUserId, userId);
        }
        Page<EducationChoicePlan> page = planService.page(startPagePlus(), queryWrapper);
        List<EducationChoicePlan> planList = page.getRecords();
        Set<Long> userIdSet = planList.stream().map(EducationChoicePlan::getUserId).collect(Collectors.toSet());
        LambdaQueryWrapper<SysUser> in = new LambdaQueryWrapper<SysUser>().in(StringUtils.isNotEmpty(userIdSet), SysUser::getUserId, userIdSet);
        List<SysUser> userList = userService.list(in);
        Map<Long, String> map = userList.stream().collect(Collectors.toMap(SysUser::getUserId, SysUser::getUserName));
        List<PlanVo> planVoList = planList.stream().map(e -> new PlanVo(e, map)).collect(Collectors.toList());
        return AjaxResult.ok().page(page, planVoList).build();
    }

    @ApiOperation("新增或编辑计划")
    @PostMapping("/addOrSave")
    public AjaxResult addOrSave(@RequestBody EducationChoicePlan plan){
        if (StringUtils.checkObjAllFieldsIsNull(plan)){
            return AjaxResult.warn("输入不能为空");
        }
        boolean saveOrUpdate = planService.saveOrUpdate(plan);
        return toAjax(saveOrUpdate);
    }

    @ApiOperation("删除考研计划")
    @PostMapping("/removeById")
    public AjaxResult removeById(Long id){
        if (StringUtils.isNull(id)){
            return AjaxResult.warn("输入错误");
        }
        boolean remove = planService.removeById(id);
        return toAjax(remove);
    }
}
